home *** CD-ROM | disk | FTP | other *** search
/ SGI Hot Mix 17 / Hot Mix 17.iso / HM17_SGI / research / external / sharelib / ftn_strarr_sun.f < prev    next >
Encoding:
Text File  |  1997-07-08  |  5.4 KB  |  204 lines

  1. C
  2. C    $Id: ftn_strarr_sun.f,v 1.2 1997/01/28 22:42:54 kirk Exp $
  3. C
  4. C NAME:
  5. C     ftn_strarr_sun.f    
  6. C
  7. C PURPOSE:
  8. C    This Fortran function is used to demonstrate how IDL can
  9. C    pass a string array to a Fortran routine, how that array can 
  10. C    then be converted into a Fortran array, how the IDL array contents
  11. C    can be changed and how these changes are returned to IDL. 
  12. C
  13. C CATEGORY:
  14. C    Dynamic Link
  15. C
  16. C CALLING SEQUENCE:
  17. C       This function is called in IDL by using the following command:
  18. C
  19. C    IDL> flag=CALL_EXTERNAL("ftn_strarr_sun.so", "_strarr_",   $
  20. C    IDL>                         n_elements(str_arr),str_arr)
  21. C
  22. C INPUTS:
  23. C
  24. C    n_ele:        The number of elements in the string array
  25. C
  26. C    str_arr:    The IDL string array.
  27. C      
  28. C OUTPUTS:
  29. C    The value of the each element of the String array is changed.
  30. C
  31. C SIDE EFFECTS:
  32. C    The passed in value of the IDL array is printed to stdout.
  33. C
  34. C RESTRICTIONS:
  35. C    This example is setup to run using the Sun operating system. This
  36. C    does not include a system running solaris. 
  37. C
  38. C EXAMPLE:
  39. C-----------------------------------------------------------------------------
  40. C;; The following are the commands that would be used to call this
  41. C;; routine in IDL.
  42. C;;
  43. C     str_arr     = sindgen(10)+" IDL string"
  44. C     n_el         = n_elements(str_arr)
  45. C        result = CALL_EXTERNAL('ftn_strarr_sun.so', '_str_arr_',       $
  46. C                              n_el, str_arr) 
  47. C
  48. C-----------------------------------------------------------------------------
  49. C
  50. C MODIFICATION HISTORY:
  51. C    Written October, 1993        KDB
  52. C
  53. C     Declare the Fortran function that is called by IDL via the 
  54. C    CALL_EXTERNAL Function.
  55. C
  56. C=============================================================================
  57. C$Function STR_ARR
  58.  
  59.         SUBROUTINE STR_ARR(ARGC, ARGV)
  60.  
  61. C PURPOSE:
  62. C
  63. C       Example Fortran function that is called directly from IDL via
  64. C       the CALL_EXTERNAL function. This subroutien is used to convert
  65. C    an IDL string array into a Fortran string array.
  66. C
  67. C       Declare the passed in variables
  68.  
  69.         INTEGER*4               ARGC    !Argument count
  70.         INTEGER*4               ARGV(*) !Vector of pointers to argments
  71.  
  72. C       Declare the function that will be called so that we can convert the
  73. C       IDL passed variables (ARGV) to Fortran varialbes via the parameter
  74. C       passing function %VAL().
  75.  
  76. C       Local variables
  77.  
  78.         INTEGER                 ARG_CNT
  79.  
  80. C       The argument count is passed in by value. Get the location of
  81. C       this value in memory (a pointer) and convert it into an
  82. C       Fortran integer.
  83.  
  84.         ARG_CNT = LOC(ARGC)
  85.  
  86. C    Insure that we got the correct number of arguments
  87.  
  88.     IF(ARG_CNT .ne. 2)THEN
  89.  
  90.        WRITE(*,*)'str_arr: Incorrect number of arguments'
  91.        RETURN
  92.  
  93.     ENDIF
  94.  
  95. C       To convert the pointers to the IDL variables contained in ARGV
  96. C       we must use the Fortran function %VAL. This funcion is used
  97. C       in the argument list of a Fortran sub-program. Call the Fortran
  98. C       subroutine that will actually perform the desired operations.
  99. C       Set the return value to the value of this function.
  100.  
  101.         CALL STR_ARR1( %val(ARGV(1)), %val(ARGV(2)) )
  102.  
  103. C       Thats all, return to IDL.
  104.  
  105.         RETURN
  106.  
  107.         END
  108.  
  109. C=============================================================================
  110. C$Function STR_ARR1
  111.  
  112.           SUBROUTINE STR_ARR1(N_ELEMENTS, STRARR)
  113.     
  114. C    Declare a Fortran Record type that has the same form as the 
  115. C    IDL C struct STRING. While Fortran records are not part of 
  116. C    F77, most compilers have this option.
  117. C
  118. C       Declare the string structure
  119.  
  120.     STRUCTURE /STRING/
  121.         INTEGER*2 SLEN
  122.         INTEGER*2 STYPE
  123.         INTEGER*4 S
  124.     END STRUCTURE
  125.  
  126. C    Declare a Fortran Parameter for the size of the fortran array
  127.  
  128.     INTEGER            ARR_SIZE
  129.     PARAMETER    (    ARR_SIZE    =     20  )
  130.  
  131. C    Declare a parameter for the length of the Fortran character strings
  132.  
  133.     INTEGER            CHAR_SIZE
  134.     PARAMETER    (    CHAR_SIZE    =     100 )
  135.  
  136. C    Now declare the passed in variables
  137.  
  138.     INTEGER*4        N_ELEMENTS       !Size of array
  139.  
  140.         RECORD /STRING/        STRARR(N_ELEMENTS) !The string array
  141.  
  142. C    Declare local variables
  143.  
  144.     INTEGER            I        !Counter
  145.     
  146.     CHARACTER*(CHAR_SIZE)    TMPSTR        !A temp string variable
  147.  
  148.     CHARACTER*(CHAR_SIZE)    F_STRARR(ARR_SIZE) 
  149.  
  150. C    Write a message Indicating we are in the Fortran routine
  151.  
  152.         WRITE(*,20)
  153.  20     FORMAT(1X,'Inside Fortran function str_arr ',
  154.      &            '(Called from IDL using CALL_EXTERNAL)',/)
  155.  
  156. C    Use a do loop to convert the IDL string to a Fortran string.
  157. C    put that string into the Fortran character array and change 
  158. C    the contents of the Fortran string and put the new value into
  159. C    the IDL string.
  160.  
  161.     DO I=1, N_ELEMENTS 
  162.     
  163. C      Convert the IDL string to a Fortran String
  164.  
  165.       CALL IDL_2_FORT(%VAL(STRARR(I).S), STRARR(I).SLEN, TMPSTR,
  166.      &              CHAR_SIZE)
  167.  
  168. C      Now TMPSTR contains the IDL string in Fortran format. Check that 
  169. C      the size of TMPSTR is GE to the size of the IDL string.
  170.  
  171.       IF( CHAR_SIZE .ge. STRARR(I).SLEN)THEN
  172.           WRITE(*,150)TMPSTR(1:STRARR(I).SLEN)
  173.       ELSE
  174.           WRITE(*,150)TMPSTR(1:CHAR_SIZE)
  175.       ENDIF
  176.  
  177.  150      FORMAT(10X,'String Parameter:',T30,A)
  178.  
  179. C      Put this string into the Fortran String array
  180.  
  181.       IF( I .le. ARR_SIZE)   F_STRARR(I)=TMPSTR
  182.  
  183. C      Now change the IDL string 
  184.  
  185.       WRITE(TMPSTR, 2000)I
  186. 2000      FORMAT('String Index: ',I2)
  187.  
  188. C      Copy the string over to the IDL string
  189.  
  190.       CALL FORT_2_IDL(TMPSTR, %val(STRARR(I).S), STRARR(I).SLEN,
  191.      &            CHAR_SIZE) 
  192.  
  193.     END DO
  194.  
  195. C    Now we have converted the IDL string array into a Fortran string
  196. C    array, and changed the contents of the IDL string array elements.
  197. C
  198. C       Thats it, return to the calling routine
  199.  
  200.         RETURN
  201.  
  202.         END
  203.  
  204.